package javolution.xml.sax;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import javolution.lang.Reflection;
import javolution.lang.Reusable;
import javolution.text.CharArray;
import javolution.xml.stream.XMLStreamException;
import javolution.xml.stream.XMLStreamReaderImpl;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

/* loaded from: classes2.dex */
public class XMLReaderImpl implements XMLReader, Reusable {
    private ContentHandler _contentHandler;
    private DTDHandler _dtdHandler;
    private EntityResolver _entityResolver;
    private ErrorHandler _errorHandler;
    private final XMLStreamReaderImpl _xmlReader = new XMLStreamReaderImpl();
    private static DefaultHandler DEFAULT_HANDLER = new DefaultHandler();
    private static final Reflection.Constructor NEW_URL = Reflection.getConstructor("java.net.URL(java.lang.String)");
    private static final Reflection.Method OPEN_STREAM = Reflection.getMethod("java.net.URL.openStream()");
    private static final Reflection.Constructor NEW_FILE_INPUT_STREAM = Reflection.getConstructor("java.io.FileInputStream(java.lang.String)");
    private static final CharArray NO_CHAR = new CharArray("");

    public XMLReaderImpl() {
        setContentHandler(DEFAULT_HANDLER);
        setErrorHandler(DEFAULT_HANDLER);
    }

    private void parseAll() throws XMLStreamException, SAXException {
        if (this._xmlReader.getEventType() != 7) {
            throw new SAXException("Currently parsing");
        }
        this._contentHandler.startDocument();
        boolean z = true;
        while (z) {
            int next = this._xmlReader.next();
            int i = 0;
            if (next != 8) {
                if (next != 12) {
                    switch (next) {
                        case 1:
                            int namespaceCount = this._xmlReader.getNamespaceCount();
                            while (i < namespaceCount) {
                                CharArray namespacePrefix = this._xmlReader.getNamespacePrefix(i);
                                if (namespacePrefix == null) {
                                    namespacePrefix = NO_CHAR;
                                }
                                this._contentHandler.startPrefixMapping(namespacePrefix, this._xmlReader.getNamespaceURI(i));
                                i++;
                            }
                            CharArray namespaceURI = this._xmlReader.getNamespaceURI();
                            if (namespaceURI == null) {
                                namespaceURI = NO_CHAR;
                            }
                            this._contentHandler.startElement(namespaceURI, this._xmlReader.getLocalName(), this._xmlReader.getQName(), this._xmlReader.getAttributes());
                            break;
                        case 2:
                            CharArray namespaceURI2 = this._xmlReader.getNamespaceURI();
                            if (namespaceURI2 == null) {
                                namespaceURI2 = NO_CHAR;
                            }
                            this._contentHandler.endElement(namespaceURI2, this._xmlReader.getLocalName(), this._xmlReader.getQName());
                            int namespaceCount2 = this._xmlReader.getNamespaceCount();
                            while (i < namespaceCount2) {
                                CharArray namespacePrefix2 = this._xmlReader.getNamespacePrefix(i);
                                if (namespacePrefix2 == null) {
                                    namespacePrefix2 = NO_CHAR;
                                }
                                this._contentHandler.endPrefixMapping(namespacePrefix2);
                                i++;
                            }
                            break;
                        case 3:
                            this._contentHandler.processingInstruction(this._xmlReader.getPITarget(), this._xmlReader.getPIData());
                            break;
                        case 6:
                            CharArray text = this._xmlReader.getText();
                            this._contentHandler.ignorableWhitespace(text.array(), text.offset(), text.length());
                            break;
                    }
                }
                CharArray text2 = this._xmlReader.getText();
                this._contentHandler.characters(text2.array(), text2.offset(), text2.length());
            } else {
                this._xmlReader.close();
                z = false;
            }
        }
    }

    @Override // javolution.xml.sax.XMLReader
    public ContentHandler getContentHandler() {
        ContentHandler contentHandler = this._contentHandler;
        if (contentHandler == DEFAULT_HANDLER) {
            return null;
        }
        return contentHandler;
    }

    @Override // javolution.xml.sax.XMLReader
    public DTDHandler getDTDHandler() {
        return this._dtdHandler;
    }

    @Override // javolution.xml.sax.XMLReader
    public EntityResolver getEntityResolver() {
        return this._entityResolver;
    }

    @Override // javolution.xml.sax.XMLReader
    public ErrorHandler getErrorHandler() {
        ErrorHandler errorHandler = this._errorHandler;
        if (errorHandler == DEFAULT_HANDLER) {
            return null;
        }
        return errorHandler;
    }

    @Override // javolution.xml.sax.XMLReader
    public boolean getFeature(String str) throws SAXNotRecognizedException, SAXNotSupportedException {
        if (str.equals("http://xml.org/sax/features/namespaces") || str.equals("http://xml.org/sax/features/namespace-prefixes")) {
            return true;
        }
        throw new SAXNotRecognizedException("Feature " + str + " not recognized");
    }

    @Override // javolution.xml.sax.XMLReader
    public Object getProperty(String str) throws SAXNotRecognizedException, SAXNotSupportedException {
        throw new SAXNotRecognizedException("Property " + str + " not recognized");
    }

    public void parse(InputStream inputStream) throws IOException, SAXException {
        try {
            try {
                this._xmlReader.setInput(inputStream);
                parseAll();
            } catch (XMLStreamException e) {
                if (!(e.getNestedException() instanceof IOException)) {
                    throw new SAXException(e);
                }
                throw ((IOException) e.getNestedException());
            }
        } finally {
            this._xmlReader.reset();
        }
    }

    public void parse(InputStream inputStream, String str) throws IOException, SAXException {
        try {
            try {
                this._xmlReader.setInput(inputStream, str);
                parseAll();
            } catch (XMLStreamException e) {
                if (!(e.getNestedException() instanceof IOException)) {
                    throw new SAXException(e);
                }
                throw ((IOException) e.getNestedException());
            }
        } finally {
            this._xmlReader.reset();
        }
    }

    public void parse(Reader reader) throws IOException, SAXException {
        try {
            try {
                this._xmlReader.setInput(reader);
                parseAll();
            } catch (XMLStreamException e) {
                if (!(e.getNestedException() instanceof IOException)) {
                    throw new SAXException(e);
                }
                throw ((IOException) e.getNestedException());
            }
        } finally {
            this._xmlReader.reset();
        }
    }

    @Override // javolution.xml.sax.XMLReader
    public void parse(String str) throws IOException, SAXException {
        InputStream inputStream;
        try {
            try {
                inputStream = (InputStream) OPEN_STREAM.invoke(NEW_URL.newInstance(str));
            } catch (Exception unused) {
                throw new UnsupportedOperationException("Cannot parse " + str);
            }
        } catch (Exception unused2) {
            inputStream = (InputStream) NEW_FILE_INPUT_STREAM.newInstance(str);
        }
        parse(inputStream);
    }

    @Override // javolution.xml.sax.XMLReader
    public void parse(InputSource inputSource) throws IOException, SAXException {
        Reader characterStream = inputSource.getCharacterStream();
        if (characterStream != null) {
            parse(characterStream);
            return;
        }
        InputStream byteStream = inputSource.getByteStream();
        if (byteStream != null) {
            parse(byteStream, inputSource.getEncoding());
        } else {
            parse(inputSource.getSystemId());
        }
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        setContentHandler(DEFAULT_HANDLER);
        setErrorHandler(DEFAULT_HANDLER);
        this._xmlReader.reset();
    }

    @Override // javolution.xml.sax.XMLReader
    public void setContentHandler(ContentHandler contentHandler) {
        if (contentHandler == null) {
            throw new NullPointerException();
        }
        this._contentHandler = contentHandler;
    }

    @Override // javolution.xml.sax.XMLReader
    public void setDTDHandler(DTDHandler dTDHandler) {
        this._dtdHandler = dTDHandler;
    }

    @Override // javolution.xml.sax.XMLReader
    public void setEntityResolver(EntityResolver entityResolver) {
        this._entityResolver = entityResolver;
    }

    @Override // javolution.xml.sax.XMLReader
    public void setErrorHandler(ErrorHandler errorHandler) {
        if (errorHandler == null) {
            throw new NullPointerException();
        }
        this._errorHandler = errorHandler;
    }

    @Override // javolution.xml.sax.XMLReader
    public void setFeature(String str, boolean z) throws SAXNotRecognizedException, SAXNotSupportedException {
        if (str.equals("http://xml.org/sax/features/namespaces") || str.equals("http://xml.org/sax/features/namespace-prefixes")) {
            return;
        }
        throw new SAXNotRecognizedException("Feature " + str + " not recognized");
    }

    @Override // javolution.xml.sax.XMLReader
    public void setProperty(String str, Object obj) throws SAXNotRecognizedException, SAXNotSupportedException {
        throw new SAXNotRecognizedException("Property " + str + " not recognized");
    }
}
